<?php
class UsersController extends AppController {

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator', 'Session');
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout');
    }
    public function admin_dashboard() {
        $title_for_layout = 'Dashboard';
        $this->set(array('title_for_layout' =>  $title_for_layout));
        
    }
    public function admin_index() {
        $this->set(array('title_for_layout' =>  'All user'));
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function admin_view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function admin_add() {
        $this->set(array('title_for_layout' =>  'Add new user'));
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

    public function admin_edit($id = null) {
        $this->User->id = $id;
       
        $this->set(array('title_for_layout' =>  'Edit user'));
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if(empty($this->request->data['User']['password']))
            {
                unset($this->request->data['User']['password']);
                unset($this->request->data['User']['repass']);
            }
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                if($id == $this->currentUser['id'])
                {
                    return $this->redirect(array('action' => 'admin_logout'));
                }
                else
                {
                    return $this->redirect(array('action' => 'admin_index'));
                }                
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function admin_delete($id = null) {
        $this->request->onlyAllow('post');

        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        return $this->redirect(array('action' => 'index'));
    }
    public function admin_login() {
        $this->layout = false;
        $this->set(array('title_for_layout' =>  'Login'));
        if ($this->request->is('post')) 
        {
            if ($this->Auth->login()) 
            {
                // did they select the remember me checkbox?
                // See at: http://stackoverflow.com/questions/12447487/cakephp-remember-me-with-auth
                if ($this->request->data['User']['remember'] == 1) 
                {
                    // remove "remember me checkbox"
                    unset($this->request->data['User']['remember']);

                    // hash the user's password
                    $this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);

                    // write the cookie
                    $this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '2 weeks');
                }
                return $this->redirect($this->Auth->redirect());
            }
            else
            {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

    public function admin_logout() {
        return $this->redirect($this->Auth->logout());
    }

}